﻿@model CleverCMS.Blog.Services.ViewModels.BlogRatingViewModel

@functions
{
    public static string Check(double lower, double upper, double toCheck)
    {
        return toCheck > lower && toCheck <= upper ? " checked=\"checked\"" : null;
    }
}

@if (User.Identity.IsAuthenticated)
{
    <script type="text/javascript">
        $(function () {
            var notRated = false;

            $.ajax({
                async: false,
                type: "POST",
                url: "@Url.Action("IsRated","Tools")",
                data: {
                    name: $('input[name=User]').attr('value'),
                    type: $('input[name=Type]').attr('value'),
                    id: $('input[name=EntryId]').attr('value')
                },
                success: function(message) {
                    if (message.success == true)
                        notRated = !message.rated;
                }
            });

            if (notRated == true) {
                $('.star').rating('readOnly', false);
                $('#rater').hide();

                $('.star').on('click', function() {
                    var value = $(this).find('a').text();
                    var objId = $('input[name=EntryId]').attr('value');
                    var objType = $('input[name=Type]').attr('value');

                    $.ajax({
                        type: "POST",
                        url: "@Url.Action("SetRatingFixed", "Tools")",
                        data: { number: value, id: objId, type: objType },
                        success: function(response) {
                            if (response.success != false) {
                                $('#currentlyrated').html('Currently rated ' + response.totalRating + ' by ' + response.totalPeople + ' people');
                                var res = (response.totalRating / 5) * 20;
                                $('.star').rating('select', res - 1);
                                $('.star').rating('readOnly', true);
                            }
                        },
                        error: function() {
                            alert('There was an error.');
                        }
                    });
                });
            } else {
                $('.star').rating('readOnly', true);
                $('#currentlyrated').remove();
                $('#rater').hide();     
            }
            
            $('.rating-cancel').each(function() {
                $(this).remove();
            });

        });
    </script>
}
else
{
    <script type="text/javascript">
        $(function() {
            $('.star').rating('readOnly', true);
            $('#currentlyrated').remove();
            $('#rater').hide();
        });
    </script>
}

<div id="rated">
    <div style="float:left">
        <form id="Form1" method="post" action="">
            <input class="star {split:4}" type="radio" value="1" name="rating" @Check(0,0.25,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="2" name="rating" @Check(0.25,0.5,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="3" name="rating" @Check(0.5,0.75,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="4" name="rating" @Check(0.75,1,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="5" name="rating" @Check(1,1.25,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="6" name="rating" @Check(1.25,1.5,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="7" name="rating" @Check(1.5,1.75,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="8" name="rating" @Check(1.75,2,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="9" name="rating" @Check(2,2.25,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="10" name="rating" @Check(2.25,2.5,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="11" name="rating" @Check(2.5,2.75,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="12" name="rating" @Check(2.75,3,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="13" name="rating" @Check(3,3.25,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="14" name="rating" @Check(3.25,3.5,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="15" name="rating" @Check(3.5,3.75,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="16" name="rating" @Check(3.75,4,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="17" name="rating" @Check(4,4.25,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="18" name="rating" @Check(4.25,4.5,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="19" name="rating" @Check(4.5,4.75,Model.AverageRating) />
            <input class="star {split:4}" type="radio" value="20" name="rating" @Check(4.75,5,Model.AverageRating) />
        </form>
    </div>
    <p id="currentlyrated" style="float:left; padding-left:20px; padding-top: 5px;">
        @Html.Raw(Model.AverageRating > 0 ? "Currently rated " + Model.AverageRating.ToString("f") + " by " + Model.TotalRaters + " people" : "<span style=\"color:red\">Not yet rated.  Be the first to rate this article!</span>")
    </p>
</div>

<div style="clear:both"></div>

<div id="rater">
    <div style="float:left;">
        <form id="rate" method="post" action="">
            <input class="auto-submit-star" type="radio" name="score" value="1"/>
            <input class="auto-submit-star" type="radio" name="score" value="2"/>
            <input class="auto-submit-star" type="radio" name="score" value="3"/>
            <input class="auto-submit-star" type="radio" name="score" value="4"/>
            <input class="auto-submit-star" type="radio" name="score" value="5"/>
            <input type="hidden" name="EntryId" value="@Model.Id" />
            <input type="hidden" name="Type" value="@(Model.IsPage ? "page": "post")" />
            <input type="hidden" name="User" value="@User.Identity.Name" />
        </form>
    </div>
    <p style="float:left;padding-left:20px; padding-top: 5px;">Rate Now!</p>
</div>

<div style="clear:both"></div>